package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.lkt.common.validator.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.GzhMenuTemplateEntity;
import io.lkt.modules.sys.service.GzhMenuTemplateService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 公众号模板菜单
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-09-01 11:22:03
 */
@RestController
@RequestMapping("/sys/gzhmenutemplate")
public class GzhMenuTemplateController {
    @Autowired
    private GzhMenuTemplateService gzhMenuTemplateService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:gzhmenutemplate:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = gzhMenuTemplateService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:gzhmenutemplate:info")
    public R info(@PathVariable("id") Integer id){
        GzhMenuTemplateEntity gzhMenuTemplate = gzhMenuTemplateService.getById(id);

        return R.ok().put("gzhMenuTemplate", gzhMenuTemplate);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:gzhmenutemplate:save")
    public R save(@RequestBody GzhMenuTemplateEntity gzhMenuTemplate){
        gzhMenuTemplate.setCreateTime(new Date());
        gzhMenuTemplateService.save(gzhMenuTemplate);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:gzhmenutemplate:update")
    public R update(@RequestBody GzhMenuTemplateEntity gzhMenuTemplate){
        ValidatorUtils.validateEntity(gzhMenuTemplate);
        gzhMenuTemplateService.updateById(gzhMenuTemplate);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:gzhmenutemplate:delete")
    public R delete(@RequestBody Integer[] ids){
        gzhMenuTemplateService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/getGzhMenuTemplateList")
    public R getGzhMenuTemplateList() {
        List<GzhMenuTemplateEntity> list = gzhMenuTemplateService.list();
        return R.ok().put("gzhMenuTemplateList", list);
    }

    @RequestMapping("/getGzhMenuTemplateByName")
    public R getGzhMenuTemplateByName(@RequestParam String templateName) {
        if (StringUtils.isBlank(templateName)) {
            return R.error("模板不存在");
        }
        GzhMenuTemplateEntity entity = gzhMenuTemplateService.getGzhMenuTemplateByName(templateName);
        return R.ok().put("gzhMenuTemplate", entity);
    }

}
